package net.expectx.pay.controller.system;

import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.base.BaseConstants;
import net.expectx.common.single.base.BaseResult;
import net.expectx.pay.common.AdminConstants;
import net.expectx.pay.common.AdminResult;
import net.expectx.pay.common.BaseController;
import net.expectx.pay.common.util.ExpectXUtil;
import net.expectx.pay.dao.bean.SystemType;
import net.expectx.pay.dao.bean.SystemTypeExample;
import net.expectx.pay.rpc.api.SystemTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Rui
 * 2018.9.12
 */
@Controller
@RequestMapping(value = "/manager/system/systemType")
public class SystemTypeController extends BaseController {

    @Autowired
    private SystemTypeService systemTypeService;

    @ApiOperation(value = "字典主页")
    @RequestMapping(value = "/index/{id}", method = RequestMethod.GET)
    public String index(@PathVariable("id") String id,ModelMap modelMap) {
        modelMap.put("id",id);
        return "system/systemType/index";
    }

    @ApiOperation(value = "新增字典")
    @RequestMapping(value = "/create/{id}", method = RequestMethod.GET)
    public String create(@PathVariable("id") String id,ModelMap modelMap) {
        modelMap.put("id", id);
        return "system/systemType/modify";
    }

    @ApiOperation(value = "修改字典")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        SystemType systemType = systemTypeService.selectByPrimaryKey(id);
        modelMap.put("systemType", systemType);
        return "system/systemType/modify";
    }

    @RequestMapping(value = "/page/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request,@PathVariable("id")Long id) {
        SystemTypeExample systemTypeExample = new SystemTypeExample();
        SystemTypeExample.Criteria criteria = systemTypeExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        if(id!=null){
            criteria.andTypeGroupIdEqualTo(id);
        }
        String typeName = request.getParameter("typeName");
        if (StringUtils.isNotBlank(typeName)) {
            criteria.andTypeNameLike(typeName+"%");
        }
        systemTypeExample.setPageIndex(page);
        systemTypeExample.setPageSize(limit);
        systemTypeExample.setOrderByClause("gmt_modified desc");
        List<SystemType> systemTypes = systemTypeService.selectByExample(systemTypeExample);
        long total = systemTypeService.countByExample(systemTypeExample);

        Map<String, Object> result = new HashMap<>(16);
        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", systemTypes);
        return result;
    }

    @ApiOperation(value = "新增字典")
    @RequestMapping(value = "/create/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object create(@PathVariable("id")long id, SystemType systemType) {
        systemType.setGmtCreate(System.currentTimeMillis());
        systemType.setGmtModified(System.currentTimeMillis());
        systemType.setIsDeleted(BaseConstants.NO);
        systemType.setTypeGroupId(id);

        return systemTypeService.insertSelective(systemType);
    }

    @ApiOperation(value = "修改字典")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, SystemType systemType) {
        systemType.setGmtModified(System.currentTimeMillis());


        return systemTypeService.updateByPrimaryKeySelective(systemType);
    }

    @ApiOperation(value = "删除字典")
    @RequestMapping(value = "/physicalRemove/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {

        SystemTypeExample systemTypeExample=new SystemTypeExample();
        systemTypeExample.createCriteria().andTypeGroupIdIn(ExpectXUtil.toList(ids,","));
        BaseResult baseResult=systemTypeService.physicalRemoveByExample(AdminConstants.YES,systemTypeExample);
        return new AdminResult(baseResult);
    }
}
